package com.mxwl.cbg.common.calculator;


import com.mxwl.cbg.common.model.base.Gemstone;

import java.util.List;
import java.util.Map;

public class EquipmentCalculator {

    /**
     * 计算装备的初始敏捷
     *
     * @param attributes         装备字面属性
     * @param gemstones          镶嵌的宝石列表
     * @param enhanceLevel       锻炼等级
     * @param refiningEffectsMap 熔炼效果
     * @return 初始防御值
     */
    public static int calculateInitialAgile(
            Map<String, Double> attributes,
            List<String> gemstones,
            int enhanceLevel,
            Map<String, Double> refiningEffectsMap) {
        Double initialAgile = attributes.getOrDefault("敏捷", (double) 0);
        Double initialAgileBonus = refiningEffectsMap.getOrDefault("敏捷", (double) 0);
        return (int) (initialAgile + initialAgileBonus);
    }

    /**
     * 计算装备的初始气血
     *
     * @param attributes         装备字面属性
     * @param gemstones          镶嵌的宝石列表
     * @param enhanceLevel       锻炼等级
     * @param refiningEffectsMap 熔炼效果
     * @return 初始防御值
     */
    public static int calculateInitialHP(
            Map<String, Double> attributes,
            List<String> gemstones,
            int enhanceLevel,
            Map<String, Double> refiningEffectsMap) {
        //面板气血值
        Double initialHP = attributes.getOrDefault("气血", (double) 0);
        //熔炼加成
        Double initialHPBonus = refiningEffectsMap.getOrDefault("气血", (double) 0);
        int gemBonus = 0;
        if (gemstones.contains(Gemstone.GloryStone.getChineseName())) {
            gemBonus = Gemstone.GloryStone.getValue() * enhanceLevel;
        }
        return (int) (initialHP + initialHPBonus - gemBonus);
    }

    /**
     * 计算装备的初始总灵力
     *
     * @param attributes         装备字面属性
     * @param gemstones          镶嵌的宝石列表
     * @param enhanceLevel       锻炼等级
     * @param refiningEffectsMap 熔炼效果
     * @return 初始防御值
     */
    public static int calculateInitialSpiritualCount(
            Map<String, Double> attributes,
            List<String> gemstones,
            int enhanceLevel,
            Map<String, Double> refiningEffectsMap) {
        Double panelSpiritualPower = attributes.getOrDefault("灵力", (double) 0);
        Double refinedSpiritualPower = refiningEffectsMap.getOrDefault("灵力", (double) 0);
        return (int) (panelSpiritualPower + refinedSpiritualPower);
    }

    /**
     * 计算装备的初始灵力
     *
     * @param attributes         装备字面属性
     * @param gemstones          镶嵌的宝石列表
     * @param enhanceLevel       锻炼等级
     * @param refiningEffectsMap 熔炼效果
     * @return 初始防御值
     */
    public static int calculateInitialSpiritual(
            Map<String, Double> attributes,
            List<String> gemstones,
            int enhanceLevel,
            Map<String, Double> refiningEffectsMap) {
        Double panelSpiritualPower = attributes.getOrDefault("灵力", (double) 0);
        Double refinedSpiritualPower = refiningEffectsMap.getOrDefault("灵力", (double) 0);

        int gemBonus = 0;
        if (gemstones.contains(Gemstone.SheliZi.getChineseName())) {
            gemBonus = Gemstone.SheliZi.getValue() * enhanceLevel;
        }
        return (int) (panelSpiritualPower + refinedSpiritualPower - gemBonus);
    }

    /**
     * 计算装备的初始总伤害（包含命中）
     *
     * @param attributes         装备字面属性
     * @param gemstones          镶嵌的宝石列表
     * @param enhanceLevel       锻炼等级
     * @param refiningEffectsMap 熔炼效果
     * @return 初始防御值
     */
    public static int calculateInitialMaxHurt(
            Map<String, Double> attributes,
            List<String> gemstones,
            int enhanceLevel,
            Map<String, Double> refiningEffectsMap) {
        Double hurt = attributes.getOrDefault("伤害", (double) 0);
        Double hitRate = attributes.getOrDefault("命中", (double) 0);
        Double refiningHurtEffect = refiningEffectsMap.getOrDefault("伤害", (double) 0);

        //如果镶嵌了太阳石
        if (gemstones.contains(Gemstone.SunStone.getChineseName())) {
            hurt = hurt - (Gemstone.SunStone.getValue() * enhanceLevel);
        }

        //如果镶嵌了玛瑙，需要扣除命中
        if (gemstones.contains(Gemstone.RedAgate.getChineseName())) {
            hitRate = hitRate - (Gemstone.RedAgate.getValue() * enhanceLevel);
        }

        return (int) (hurt + (hitRate / 3) + refiningHurtEffect);
    }

    /**
     * 获取装备的总伤害
     *
     * @param attributes         装备字面属性
     * @param refiningEffectsMap 熔炼效果
     * @return 获取装备的总伤害
     */
    public static int calculateInitialTotalHurt(
            Map<String, Double> attributes,
            Map<String, Double> refiningEffectsMap) {
        Double hurt = attributes.getOrDefault("伤害", (double) 0);
        Double hitRate = attributes.getOrDefault("命中", (double) 0);
        Double refiningHurtEffect = refiningEffectsMap.getOrDefault("伤害", (double) 0);
        return (int) (hurt + (hitRate / 3) + refiningHurtEffect);
    }

    /**
     * 计算装备的初始伤害
     *
     * @param attributes         装备字面属性
     * @param gemstones          镶嵌的宝石列表
     * @param enhanceLevel       锻炼等级
     * @param refiningEffectsMap 熔炼效果
     * @return 初始防御值
     */
    public static int calculateInitialHurt(
            Map<String, Double> attributes,
            List<String> gemstones,
            int enhanceLevel,
            Map<String, Double> refiningEffectsMap) {
        Double hurt = attributes.getOrDefault("伤害", (double) 0);
        Double refiningHurtEffect = refiningEffectsMap.getOrDefault("伤害", (double) 0);
        int gemstoneBonus = 0;
        if (gemstones.contains(Gemstone.SunStone.getChineseName())) {
            gemstoneBonus = Gemstone.SunStone.getValue() * enhanceLevel;
        }
        return (int) (hurt - gemstoneBonus + refiningHurtEffect);
    }

    /**
     * 计算装备的初始防御值（考虑混合宝石）
     *
     * @param attributes         装备字面属性
     * @param gemstones          镶嵌的宝石列表
     * @param enhanceLevel       锻炼等级
     * @param refiningEffectsMap 熔炼效果
     * @return 初始防御值
     */
    public static int calculateInitialDefense(
            Map<String, Double> attributes,
            List<String> gemstones,
            int enhanceLevel,
            Map<String, Double> refiningEffectsMap) {

        // 1. 获取装备的字面防御值
        Double literalDefense = attributes.getOrDefault("防御", (double) 0);

        // 2. 获取熔炼效果中的防御加成（可能为负值）
        Double refiningDefenseBonus = refiningEffectsMap.getOrDefault("防御", (double) 0);

        // 3. 计算宝石对防御的加成
        int gemstoneDefenseBonus = calculateGemstoneDefenseBonus(
                gemstones, enhanceLevel, attributes
        );

        // 4. 计算初始防御值（新公式）
        // 公式：初始防御 = 字面防御 - 宝石防御加成 + 熔炼防御加成
        return (int) (literalDefense - gemstoneDefenseBonus + refiningDefenseBonus);
    }

    /**
     * 计算宝石对防御的加成（考虑混合镶嵌）
     */
    private static int calculateGemstoneDefenseBonus(
            List<String> gemstones,
            int enhanceLevel, Map<String, Double> attributes
    ) {

        // 检查是否有月亮石（加防御）
        boolean hasMoonStone = gemstones.contains("月亮石");

        // 检查是否有光芒石（加气血）
        boolean hasGloryStone = gemstones.contains("光芒石");

        // 如果只有月亮石
        if (hasMoonStone && !hasGloryStone) {
            return Gemstone.MoonStone.getValue() * enhanceLevel;
        }

        // 同时光芒石
        if (hasMoonStone && hasGloryStone) {
            Double vitality = attributes.getOrDefault("气血", (double) 0);
            int v = (int) (vitality / 40);
            return Gemstone.MoonStone.getValue() * (enhanceLevel - v); // 光芒石不加防御
        }
        // 没有防御相关宝石
        return 0;
    }

}